_xrnarchive-cli_autocomplete() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    #prev="${COMP_WORDS[COMP_CWORD-1]}"
    command=$( echo "${COMP_WORDS[@]}" | tr -s ' ' '\n' | awk '/--/{print $0}' )
    opts_generate_client_scripts="-json-command -copy-inputs-and-archive -extract-outputs-and-copy"
    opts_check_inputs="-json-command -json-log"
    opts_check_outputs="-json-command -json-log"
    opts_generate_server_scripts="-json-command -archive-outputs-script"

    # Convert space-separated strings to arrays
    read -r -a array0 <<< "$opts_generate_client_scripts"
    read -r -a array1 <<< "${COMP_WORDS[@]}"

    # Initialize an empty array to store the result
    result=()

    # Loop over each element in the first array
    for item in "${array0[@]}"; do
        # Check if the element exists in the second array
        if [[ ! " ${array1[@]} " =~ " $item " ]]; then
            # Add the element to the result array
            result+=("$item")
       fi
    done
    opts_generate_client_scripts_sub="${result[*]}"

    # Convert space-separated strings to arrays
    read -r -a array0 <<< "$opts_check_inputs"
    read -r -a array1 <<< "${COMP_WORDS[@]}"

    # Initialize an empty array to store the result
    result=()

    # Loop over each element in the first array
    for item in "${array0[@]}"; do
        # Check if the element exists in the second array
        if [[ ! " ${array1[@]} " =~ " $item " ]]; then
            # Add the element to the result array
            result+=("$item")
       fi
    done
    opts_check_inputs_sub="${result[*]}"

    # Convert space-separated strings to arrays
    read -r -a array0 <<< "$opts_check_outputs"
    read -r -a array1 <<< "${COMP_WORDS[@]}"

    # Initialize an empty array to store the result
    result=()

    # Loop over each element in the first array
    for item in "${array0[@]}"; do
        # Check if the element exists in the second array
        if [[ ! " ${array1[@]} " =~ " $item " ]]; then
            # Add the element to the result array
            result+=("$item")
       fi
    done
    opts_check_outputs_sub="${result[*]}"

    # Convert space-separated strings to arrays
    read -r -a array0 <<< "$opts_generate_server_scripts"
    read -r -a array1 <<< "${COMP_WORDS[@]}"

    # Initialize an empty array to store the result
    result=()

    # Loop over each element in the first array
    for item in "${array0[@]}"; do
        # Check if the element exists in the second array
        if [[ ! " ${array1[@]} " =~ " $item " ]]; then
            # Add the element to the result array
            result+=("$item")
       fi
    done
    opts_generate_server_scripts_sub="${result[*]}"


    # Check if an option has already been used
    local used_opts=$(echo "${COMP_WORDS[@]}" | grep -oE -- '(\s|^)--[a-zA-Z0-9]+' | sort | uniq)

    case "${command}" in
        --generate-client-scripts)
            COMPREPLY=( $(compgen -W "${opts_generate_client_scripts_sub}" -- ${cur}) )
            return 0
            ;;
        --generate-server-scripts)
            COMPREPLY=( $(compgen -W "${opts_generate_server_scripts}" -- ${cur}) )
            return 0
            ;;
        --check-inputs)
            COMPREPLY=( $(compgen -W "${opts_check_inputs_sub}" -- ${cur}) )
            return 0
            ;;
        --check-outputs)
            COMPREPLY=( $(compgen -W "${opts_check_outputs}" -- ${cur}) )
            return 0
            ;;
        *)
            COMPREPLY=( $(compgen -W "--generate-client-scripts --generate-server-scripts --check-inputs --check-outputs $(echo "${opts}" | grep -v -E "${used_opts}")" -- ${cur}) )
            return 0
            ;;
    esac
}
complete -F _xrnarchive-cli_autocomplete xrnarchive-cli

